package userinterface;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

/**
 * Created by IntelliJ IDEA.
 * User: dima
 * Date: 18.09.2010
 * Time: 13:13:38
 * Для ограничения длины вводимой строки
 */
public class LengthFilter extends DocumentFilter {
    private int currentLength;
    private int maxLength;

    public LengthFilter(int currentLength, int maxLength) {
        this.currentLength = currentLength;
        this.maxLength = maxLength;
    }

    public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
        currentLength -= length;
        fb.remove(offset, length);
    }

    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
        if ((currentLength + string.length()) <= maxLength) {
            currentLength += string.length();
            fb.insertString(offset, string, attr);
        }
    }

    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException {
        if ((currentLength - length + string.length()) <= maxLength) {
            currentLength += string.length() - length;
            fb.replace(offset, length, string, attr);
        }
    }
}